package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.repository.request.QuerySpecifiedDepthRepoRequest;
import com.epoch.app.bcots.model.dto.*;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.otsplatformenhance.enums.OrganizationTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyi
 * @date 2021/10/25 9:23 下午
 */
@Component
public class SalesPlanOrganizationRepositoryImpl implements SalesPlanOrganizationRepository {
    @Resource
    private BaseDataService baseDataService;

    @Override
    @RepositoryInvoker(printLog = true)
    public List<OrganizationSDO> queryOrganizationAllDeparment(String organizationId) {
        QueryBuinessUnitAllDeparmentRequest request = new QueryBuinessUnitAllDeparmentRequest();
        request.setOrganizationCode(organizationId);
        QueryBuinessUnitAllDeparmentResponse queryBuinessUnitAllDeparmentResponse = baseDataService.queryBuinessUnitAllDeparment(request);
        if (queryBuinessUnitAllDeparmentResponse != null){
            return queryBuinessUnitAllDeparmentResponse.getResult();
        }
        return null;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public List<OrganizationSDO> querySpecifiedDepth(QuerySpecifiedDepthRepoRequest request){
        int depth = OrganizationTypeEnum.values().length;
        List<OrganizationSDO> result = Lists.newArrayList();
        getSpecifiedDepth(result,request,depth);
        return result;
    }

    private void getSpecifiedDepth(List<OrganizationSDO> result,QuerySpecifiedDepthRepoRequest request,int depth){
        if (depth <= 0){
            return;
        }
        SalesPlanGetChildrenOrgRequest byIdRequest = new SalesPlanGetChildrenOrgRequest();
        byIdRequest.setOrganizationId(request.getOrganizationId());
        SalesPlanGetChildrenOrgResponse salesPlanGetChildrenOrgResponse = baseDataService.salesPlanGetChildrenOrg(byIdRequest);
        if (salesPlanGetChildrenOrgResponse == null || CollectionUtils.isEmpty(salesPlanGetChildrenOrgResponse.getResult())){
            return;
        }
        List<OrganizationSDO> childrenOrgResponseResult = salesPlanGetChildrenOrgResponse.getResult();
        if (childrenOrgResponseResult.get(0) != null && request.getOrganizationType().equals(childrenOrgResponseResult.get(0).getType())){
            result.addAll(childrenOrgResponseResult);
            return;
        }
        depth--;
        for (OrganizationSDO organization : childrenOrgResponseResult) {
            QuerySpecifiedDepthRepoRequest repoRequest = new QuerySpecifiedDepthRepoRequest();
            repoRequest.setOrganizationType(request.getOrganizationType());
            repoRequest.setOrganizationId(organization.getId());
            getSpecifiedDepth(result,repoRequest,depth);
        }
    }
}
